package com.mindskip.xzs.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.Exam;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.domain.UserEventLog;
import com.mindskip.xzs.event.UserEvent;
import com.mindskip.xzs.service.ExamService;
import com.mindskip.xzs.viewmodel.IdNameKV;
import com.mindskip.xzs.viewmodel.admin.exam.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author artka
 * @description:
 * @date 2022/9/16 10:58 PM
 */
@RestController("ExamController")
@RequestMapping(value = "/api/admin/exam")
public class ExamController extends BaseApiController {

    @Resource
    private ExamService examService;

    @RequestMapping(value = "/changeShowAnalysis", method = RequestMethod.GET)
    private RestResponse<Boolean> changeShowAnalysisStatus(@Param("showAnalysis") Boolean showAnalysis, @Param("examId") Integer examId) {
        return RestResponse.ok(
                examService.update(
                        new LambdaUpdateWrapper<Exam>()
                                .set(Exam::getShowAnalysis, showAnalysis)
                                .eq(Exam::getId, examId)
                )
        );
    }

    @RequestMapping(value = "/changeShowCorrectAnswer", method = RequestMethod.GET)
    private RestResponse<Boolean> changeShowCorrectAnswerStatus(@Param("showCorrectAnswer") Boolean showCorrectAnswer, @Param("examId") Integer examId) {
        return RestResponse.ok(
                examService.update(
                        new LambdaUpdateWrapper<Exam>()
                                .set(Exam::getShowCorrectAnswer, showCorrectAnswer)
                                .eq(Exam::getId, examId)
                )
        );
    }

    /**
     * 查询考试分类列表
     */
    @RequestMapping(value = "/select/categorys", method = RequestMethod.GET)
    public RestResponse<List<String>> selectCategory(@RequestParam(required = false, value = "keyword") String keyword) {
        return RestResponse.ok(examService.queryCategoryListInCache(keyword));
    }

    @GetMapping("/page")
    public RestResponse<PageInfo<PageExamRes>> listEmpList(@RequestParam(required = false, value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(required = false, value = "pageSize", defaultValue = "10") Integer pageSize,
                                                           @RequestParam(required = false, value = "examId") Integer examId,
                                                           @RequestParam(required = false, value = "examStatus") Integer examStatus,
                                                           @RequestParam(required = false, value = "examCategory") String examCategory,
                                                           @RequestParam(required = false, value = "examType") Integer examType,
                                                           @RequestParam(required = false, value = "paperId") Integer paperId,
                                                           @RequestParam(required = false, value = "examStartTime") String examStartTime,
                                                           @RequestParam(required = false, value = "examEndTime") String examEndTime) {
        return RestResponse.ok(examService.queryExamList(
                new PageExamReq()
                        .setPageNum(pageNum)
                        .setPageSize(pageSize)
                        .setExamId(examId)
                        .setExamStatus(examStatus)
                        .setExamType(examType)
                        .setPaperId(paperId)
                        .setExamCategory(examCategory)
                        .setBeginTime(examStartTime)
                        .setEndTime(examEndTime)
        ));
    }

    @PostMapping("/add")
    public RestResponse<Boolean> addExam(@RequestBody @Valid AddExamReq req) {
        return RestResponse.ok(examService.addExam(req, getCurrentUser().getId()));
    }

    /**
     * 补考
     */
    @PostMapping("/rebuild")
    public RestResponse<Boolean> rebuildExam(@RequestBody @Valid RebuildExamReq req) {
        return RestResponse.ok(examService.rebuildExam(req));
    }

    @PostMapping("/addExamUser")
    public RestResponse<Boolean> addExamUser(@RequestBody @Valid AddExamUserReq req) {
        return RestResponse.ok(examService.addExamUsers(req));
    }

    @GetMapping("/del")
    public RestResponse<Boolean> delExam(@RequestParam("id") Long id,
                                         @RequestParam(required = false, value = "notice") Boolean notice) {
        return RestResponse.ok(examService.delExam(id, notice,getCurrentUser()));
    }

    @PostMapping("/edit")
    public RestResponse<Boolean> editExam(@RequestBody @Valid EditExamReq req) {
        return RestResponse.ok(examService.editExam(req, getCurrentUser().getId()));
    }

    @GetMapping("/close")
    public RestResponse<Boolean> closeExam(@RequestParam("id") Long id,
                                         @RequestParam(required = false, value = "notice") Boolean notice) {
        return RestResponse.ok(examService.closeExam(id, notice));
    }

    @GetMapping("/open")
    public RestResponse<Boolean> openExam(@RequestParam("id") Long id,
                                           @RequestParam(required = false, value = "notice") Boolean notice) {
        return RestResponse.ok(examService.openExam(id, notice));
    }

    /**
     * 查询未考人员列表
     */
    @RequestMapping(value = "/list/unexamined", method = RequestMethod.GET)
    public RestResponse<List<IdNameKV>> queryUnexaminedList(@RequestParam(value = "examId") Integer examId) {
        return RestResponse.ok(examService.queryUnexaminedList(examId));
    }

    /**
     * 查询未考人员列表
     */
    @RequestMapping(value = "/list/unexamined/notice", method = RequestMethod.GET)
    public RestResponse<Boolean> noticeUnexamineds(@RequestParam(value = "examId") Integer examId) {
        return RestResponse.ok(examService.noticeUnexaminedList(examId));
    }

    /**
     * 查询考试kv列表
     */
    @RequestMapping(value = "/list/kv", method = RequestMethod.GET)
    public RestResponse<List<IdNameKV>> queryExamKVListInCache(@RequestParam(required = false, value = "keyword") String keyword) {
        return RestResponse.ok(examService.queryExamKVListInCache(keyword));
    }
}
